<script setup lang="ts">
import {onMounted, ref} from "vue";
import {Admin} from "../../interfaces/Admin.ts";
import {Result} from "../../interfaces/Result.ts";
import {adminRegister} from "../../apis/AdminApi.js"
import {elPrompt} from "../../utils/elPrompt.ts";
import router from "../../router/router.js";
import getLocalStorage from "../../utils/getLocalStorage.js"

const admin = ref<Admin>({
  password: "",
  username: ""
})

function register() {
  adminRegister(admin.value, (result:Result<any>) => {
    if (result.status !== 200) return elPrompt(result.message, "error")
    elPrompt('注册成功', "success")
    router.push('/admin/login')
  })
}

function toLogin() {
  router.push('/admin/login')
}
</script>

<template>
  <div class="register">
    <div class="register-box">
      <h2>管理员注册</h2>
      <div class="form">
        <el-form>
          <el-form-item label="管理员用户名" prop="username">
            <el-input v-model="admin.username" type="text"/>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="admin.password" type="password" show-password/>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="admin.email" type="text"/>
          </el-form-item>
        </el-form>
        <el-button type="primary" @click="register">注册</el-button>
        <el-button type="warning" @click="toLogin">登录</el-button>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.register {
  width: 100%;
  height: 100vh;
  display: flex;
  .register-box {
    width: 500px;
    height: 400px;
    margin: auto;
    border: solid 1px var(--color-info);
    box-sizing: border-box;
    border-radius: 10px;
    overflow: hidden;
    display: flex;
    flex-direction: column;

    h2 {
      margin: 0 auto;
    }

    .form {
      margin: auto;
    }

    .el-button {
      width: 100%;
      margin: 10px auto;
    }
  }
}
</style>